<template>
  <el-container>
    <el-aside width="200px">
      <div class="top">
        <img src="https://s.weituibao.com/1582958061265/mlogo.png" alt="" />
        <p>vue3 admin</p>
      </div>
      .
      <el-menu
        active-text-color="#ffd04b"
        background-color="#222832"
        class="el-menu-vertical-demo"
        :default-active="$route.path"
        text-color="#fff"
        router
      >
        <el-sub-menu
          :index="item.name"
          v-for="(item, index) in arr"
          :key="index"
        >
          <template #title>
            <span>{{ item.name }}</span>
          </template>
          <el-menu-item
            v-for="(o, i) in item.children"
            :key="index"
            :index="o.path"
          >
            {{ o.name }}
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <div>
          <p
            style="display: flex; align-items: center"
            v-if="$route.path == '/category/level2'"
          >
            <el-button
              @click="router.go(-1)"
              round
              type="default"
              :icon="ArrowLeft"
            />分类二级管理
          </p>
          <p
            style="display: flex; align-items: center"
            v-else-if="$route.path == '/category/level3'"
          >
            <el-button
              @click="router.go(-1)"
              round
              type="default"
              :icon="ArrowLeft"
            />分类三级级管理
          </p>
          <p v-else-if="$route.path == '/order_detail'">
            <el-button
              @click="router.go(-1)"
              round
              type="default"
              :icon="ArrowLeft"
            />订单详情
          </p>
          <p v-else-if="$route.path == '/add'">
            {{ $route.query.id == undefined ? "添加商品" : "编辑商品" }}
          </p>
          <p v-else>{{ $route.meta.title }}</p>
        </div>
        <div class="pover" @click="show">
          <el-icon><UserFilled /></el-icon>
          <p>{{ userInfo?.nickName }}</p>
          <el-icon><ArrowDownBold /></el-icon>
        </div>
        <transition>
          <div class="box" v-if="showModal">
            <div class="left">
              <p>登录名：{{ userInfo?.loginUserName }}</p>
              <p>昵称：{{ userInfo?.nickName }}</p>
            </div>
            <div class="right">
              <el-button type="primary" size="small" @click="logout"
                >退出登录</el-button
              >
            </div>
          </div>
        </transition>
      </el-header>
      <el-main><router-view></router-view></el-main>
    </el-container>
  </el-container>
</template>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { UserFilled, ArrowDownBold } from "@element-plus/icons-vue";
import type { profileRes } from "../../types/type";
import { ElMessage } from "element-plus";
import "element-plus/es/components/message/style/css";
import { api } from "../../api/api";
import { ArrowLeft } from "@element-plus/icons-vue";
const router = useRouter();
const arr = [
  {
    name: "Dashboard",
    path: "",
    icon: "",
    children: [
      {
        name: "系统介绍",
        path: "/introduce",
        icon: "Menu",
      },
      {
        name: "Dashboard",
        path: "/dashboard",
        icon: "Menu",
      },
      {
        name: "添加商品",
        path: "/add",
        icon: "Menu",
      },
    ],
  },
  {
    name: "首页配置",
    path: "",
    icon: "",
    children: [
      {
        name: "轮播图配置",
        path: "/swiper",
        icon: "PictureFilled",
      },
      {
        name: "热销商品配置",
        path: "/hot",
        icon: "StarFilled",
      },
      {
        name: "新品上线配置",
        path: "/new",
        icon: "Handbag",
      },
      {
        name: "为你推荐配置",
        path: "/recommend",
        icon: "Menu",
      },
    ],
  },
  {
    name: "模块管理",
    path: "",
    icon: "",
    children: [
      {
        name: "分类管理",
        path: "/category",
        icon: "Menu",
      },
      {
        name: "商品管理",
        path: "/good",
        icon: "Menu",
      },
      {
        name: "会员管理",
        path: "/guest",
        icon: "Menu",
      },
      {
        name: "订单管理",
        path: "/order",
        icon: "Menu",
      },
    ],
  },
  {
    name: "系统管理",
    path: "",
    icon: "",
    children: [
      {
        name: "修改密码",
        path: "/account",
        icon: "Menu",
      },
    ],
  },
];
const userInfo = ref<profileRes>();
onMounted(async () => {
  const res: any = await api.profileApi();
  userInfo.value = res.data;
});
// 控制显示影藏
const showModal = ref(false);
function show() {
  if (showModal.value) showModal.value = false;
  else showModal.value = true;
}
// 退出登录
async function logout() {
  const res: any = await api.logoutApi();
  if (res.resultCode == 200) {
    localStorage.removeItem("token");
    ElMessage.success("退出成功");
    router.push("/");
  }
}
</script>

<style lang="scss" scoped>
.v-enter,
.v-leave-to {
  opacity: 0;
  transform: translateX(80px);
}
.v-enter-active,
.v-leave-active {
  transition: all 0.5s ease;
}
.box {
  width: 400px;
  height: 100px;
  background-color: pink;
  position: absolute;
  top: 60px;
  display: flex;
  right: 10px;
  padding: 20px;
  align-items: flex-start;
  justify-content: space-between;
  background: url("https://s.yezgea02.com/lingling-h5/static/account-banner-bg.png"),
    no-repeat;
  background-size: 100% 100%;
  transition: all 0.5s;
  .left {
    color: #fff;
    p:nth-child(1) {
      margin-bottom: 10px;
    }
  }
}
.el-header {
  position: relative;
  border: 1px solid #eee;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .pover {
    display: flex;
    align-items: center;
    cursor: pointer;
  }
}
.el-menu {
  border-right: none;
}
.el-aside {
  height: 100vh;
  background-color: #222832;
  overflow-y: scroll;
  &::-webkit-scrollbar {
    display: none; /* Chrome Safari */
  }
  .top {
    display: flex;
    align-items: center;
    color: #fff;
    width: 100%;
    justify-content: space-between;
    padding: 0 10px;
    img {
      width: 70px;
      height: 70px;
    }
    p {
      font-size: 18px;
      font-weight: 600;
    }
  }
}
.el-main {
  height: calc(100vh - 60px);
  overflow-y: scroll;
}
</style>
